<?php
/*Created by Timothy Cheung*/
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class details extends CI_Controller {

/*
|--------------------------------------------------------------------------
| getDetails (@author : Timothy Cheung)
|--------------------------------------------------------------------------
| This function returns the parsed item details as JSON and call the 
| function to wrap the response in the requested callback. 
*/      
    
    public function getDetails($set_no, $set_entry){
        //details_model is set into controller
        $this->load->model('details_model');
        
        //Load XML returned by X-services API into simplexml format
        $xml = 'http://libcat.tp.edu.sg/X?op=present&set_no='.$set_no.'&set_entry='.$set_entry.'&format=marc';
        $load_xml = simplexml_load_file($xml);
        
        //Parse load_xml into a details_model record, return as output
        $output = $this -> details_model -> parseDetailsXML($load_xml);
        $this -> retJsonP($output);
    }

/*
|--------------------------------------------------------------------------
| retJsonP (@author : Justina Lee)
|--------------------------------------------------------------------------
|
| This function wraps the response in the requested callback. 
|
*/
    function retJsonP($output) {
        $callback = $_REQUEST['callback'];

        if ($callback) {
            header('Content-Type: text/javascript');
            echo $callback . '(' . json_encode($output) . ');';
        } else {
            header('Content-Type: application/x-json');
            echo json_encode($output);
        }
    }

}
?>